/*
 *	OInterval
 *  Mat Ryer
 * 
 *  Simple way to manage repetative tasks
 *
 *	OInterval( key, function, interval, mode ) 
 *
 *	key					-	The unique name for this interval
 *	function			-	The callback function to call at intervals
 * 	interval			-	How long to wait between calls
 *	mode				-	How OInterval should behave
 *							OIntervalDefault 	- Default mode
 *							OIntervalImmediate 	- Triggers the function right away instead of waiting first
 *							OIntervalIdle		- Does not trigger the interval until told to by another call to OInterval
 *
 */

var 
	// array to hold the data
	__OIntervals = [],
	
	// modes
	OIntervalDefault = 0, 
	OIntervalImmediate = 1,
	OIntervalIdle = 2
;

var OInterval = function(k, f, i, m){
	
	// setup default mode
	m = m || OIntervalDefault;

	// create the object if this is new
	if (f) { __OIntervals[k] = { f: f, i: i } };

	// if we have no object, throw an exception
	if (!__OIntervals[k]) { OError("OInterval", "No OInterval called '" + k + "'.") };
	
	if (m != OIntervalIdle) {
		
		__OIntervals[k].t = window.setTimeout(__OIntervals[k].f, (m == OIntervalImmediate ? 0 : __OIntervals[k].i));
		
	}

};

OProvides("objx.OInterval");